//
//  DataBaseSingleton.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DataBaseSingleton.h"
#import "FMDatabase.h"

@interface DataBaseSingleton ()

@property(nonatomic,retain)FMDatabase *db;

@end

@implementation DataBaseSingleton

- (void)dealloc
{
    [_db release];
    [super dealloc];
}

+ (instancetype)shareDataBase{
    static DataBaseSingleton *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DataBaseSingleton alloc] init];
    }
    return dataBase;
}

-(void)openDB
{
    //导入sqlite框架，导入FMDB文件夹
#pragma mark -- 1.获得数据库文件的路径
    NSString *sqlitePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"douban.sqlite"];
    NSLog(@"fileName = %@",sqlitePath);
#pragma mark -- 2.获得数据库
    self.db = [FMDatabase databaseWithPath:sqlitePath];
#pragma mark -- 3.打开数据库
    if ([self.db open]) {
        NSLog(@"ok");
#pragma mark -- 4.创表
    // 电影列表
        BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS movieList666 (id INTEGER PRIMARY KEY AUTOINCREMENT, movieName TEXT NOT NULL, pic_url TEXT NOT NULL, movieId TEXT NOT NULL);"];
        
        if (result) {
            NSLog(@"创表成功");
        } else {
            NSLog(@"创表失败");
        }
    // 用户
        BOOL result1=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS user666 (id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL, phoneNumber TEXT NOT NULL);"];
        
        if (result1) {
            NSLog(@"创表成功");
        } else {
            NSLog(@"创表失败");
        }
    // 收藏
        BOOL result2=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS collect666 (id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT NOT NULL, title TEXT NOT NULL);"];
        
        if (result2) {
            NSLog(@"创表成功");
        } else {
            NSLog(@"创表失败");
        }
    }
}
/******************************* 插入 ************************************/
#pragma mark -- 插入数据(电影)
-(void)insertMovie:(MovieList *)movie
{
    //如果用stringWithFormat时,sql语句中可以用'%@'
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO movieList666 (movieName, pic_url, movieId) VALUES ('%@', '%@', '%@')", movie.movieName, movie.pic_url, movie.movieId];
    
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入成功");
    }
}
#pragma mark -- 插入数据(用户)
-(void)insertUser:(User *)user
{
    //如果用stringWithFormat时,sql语句中可以用'%@'
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO user666 (userName, password, email, phoneNumber) VALUES ('%@', '%@', '%@', '%@')", user.userName, user.password, user.email, user.phoneNumber];
    
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入成功");
    }
}
#pragma mark -- 插入数据(电影收藏)
-(void)inserUserName:(NSString *)userName Title:(NSString *)title
{
    //如果用stringWithFormat时,sql语句中可以用'%@'
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO collect666 (userName, title) VALUES ('%@', '%@')", userName, title];
    
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入成功");
    }
}
/******************************* 删除 ************************************/
#pragma mark -- 删除数据(电影)
-(void)deleteMovieId:(NSString *)movieId
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM movieList666 WHERE movieId = '%@'", movieId];
    BOOL res = [self.db executeUpdate:deleteSql];
    
    if (res) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
#pragma mark -- 删除数据(电影收藏)
-(void)deleteUserName:(NSString *)userName Title:(NSString *)title
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM collect666 WHERE userName = '%@' AND title = '%@'", userName, title];
    BOOL res = [self.db executeUpdate:deleteSql];
    
    if (res) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}
/******************************* 查询 ************************************/
#pragma mark 查询数据(电影)
-(NSMutableArray *)selectMovie
{
    NSMutableArray *array = [NSMutableArray array];
    //1.执行查询语句
    FMResultSet *resultSet = [self.db executeQuery:@"select * from movieList666"];
    //2.遍历结果集合
    while ([resultSet next]) {
        NSString *movieName = [resultSet objectForColumnName:@"movieName"];
        NSString *pic_url = [resultSet objectForColumnName:@"pic_url"];
        NSString *movieId = [resultSet objectForColumnName:@"movieId"];
        MovieList *movieL = [[MovieList alloc] init];
        movieL.movieName = movieName;
        movieL.pic_url = pic_url;
        movieL.movieId = movieId;
        [array addObject:movieL];
        [movieL release];
    }
    return array;
}
#pragma mark 查询数据(电影收藏传值到电影详情)
-(NSMutableArray *)selectMovieName:(NSString *)movieName
{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"select * from movieList666 WHERE movieName = '%@'", movieName];
    //1.执行查询语句
    FMResultSet *resultSet = [self.db executeQuery:sql];
    //2.遍历结果集合
    while ([resultSet next]) {
        NSString *movieName = [resultSet objectForColumnName:@"movieName"];
        NSString *pic_url = [resultSet objectForColumnName:@"pic_url"];
        NSString *movieId = [resultSet objectForColumnName:@"movieId"];
        MovieList *movieL = [[MovieList alloc] init];
        movieL.movieName = movieName;
        movieL.pic_url = pic_url;
        movieL.movieId = movieId;
        [array addObject:movieL];
        [movieL release];
    }
    return array;
}
#pragma mark 查询数据(用户)
-(NSMutableArray *)selectUserName:(NSString *)userName
{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"select * from user666 WHERE userName = '%@'", userName];
    //1.执行查询语句
    FMResultSet *resultSet = [self.db executeQuery:sql];
    //2.遍历结果集合
    while ([resultSet next]) {
        NSString *userName = [resultSet objectForColumnName:@"userName"];
        NSString *password = [resultSet objectForColumnName:@"password"];
        NSString *email = [resultSet objectForColumnName:@"email"];
        NSString *phoneNumber = [resultSet objectForColumnName:@"phoneNumber"];
        User *user = [[User alloc] init];
        user.userName = userName;
        user.password = password;
        user.email = email;
        user.phoneNumber = phoneNumber;
        [array addObject:user];
        [user release];
    }
    return array;
}
#pragma mark 查询数据(通过用户查收藏)
-(NSMutableArray *)selectCollectUserName:(NSString *)userName
{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"select * from collect666 WHERE userName = '%@'", userName];
    //1.执行查询语句
    FMResultSet *resultSet = [self.db executeQuery:sql];
    //2.遍历结果集合
    while ([resultSet next]) {
        NSString *title = [resultSet objectForColumnName:@"title"];
        MovieDetail *movieD = [[MovieDetail alloc] init];
        movieD.title = title;
        [array addObject:movieD.title];
        [movieD release];
    }
    return array;
}

@end
